package com.hd.trans.utils.thread;

import android.os.Handler;
import android.os.Looper;

import androidx.annotation.NonNull;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 线程工具类
 */
public class ThreadWrapper {

    private final Handler mainHandler = new Handler(Looper.getMainLooper());

    private final ExecutorService mDiskIO = Executors.newFixedThreadPool(4, new ThreadFactory() {
        private static final String THREAD_NAME_STEM = "arch_disk_io_%d";
        private final AtomicInteger mThreadId = new AtomicInteger(0);
        @Override
        public Thread newThread(Runnable r) {
            Thread thread = new Thread(r);
            String name = String.format(THREAD_NAME_STEM, mThreadId.getAndIncrement());
            thread.setName(name);
            return thread;
        }
    });

    private static ThreadWrapper sInstance;

    @NonNull
    private static ThreadWrapper getInstance() {
        if (sInstance != null) {
            return sInstance;
        }
        synchronized (ThreadWrapper.class) {
            if (sInstance == null) {
                sInstance = new ThreadWrapper();
            }
        }
        return sInstance;
    }

    private void executeOnMainThread(@NonNull Runnable runnable) {
        if (isMainThread()) {
            runnable.run();
        } else {
            mainHandler.post(runnable);
        }
    }

    private void executeOnMainThreadDelayed(@NonNull Runnable runnable, long delayMillis) {
        if (delayMillis <= 0) {
            executeOnMainThread(runnable);
        } else {
            mainHandler.postDelayed(runnable,delayMillis);
        }
    }

    public static boolean isMainThread() {
        return Thread.currentThread() == Looper.getMainLooper().getThread();
    }

    /**
     * UI线程执行
     */
    public static void uiThreadExecute(Runnable runnable) {
        getInstance().executeOnMainThread(runnable);
    }

    /**
     * UI线程延迟执行
     */
    public static void uiThreadExecuteDelayed(Runnable runnable, long delayMillis) {
        getInstance().executeOnMainThreadDelayed(runnable,delayMillis);
    }

    /**
     * UI线程延迟500毫秒执行
     */
    public static void uiThreadExecuteDelayed500(Runnable runnable) {
        getInstance().executeOnMainThreadDelayed(runnable,500);
    }

    /**
     * 自定义UI线程执行
     */
    public static void customUiThreadExecute(Runnable runnable) {
        CustomUiThread.getInstance().post(runnable);
    }

    /**
     * 自定义UI线程延迟执行
     */
    public static void customUiThreadExecuteDelayed(Runnable runnable,long delayMillis) {
        CustomUiThread.getInstance().postDelayed(runnable,delayMillis);
    }

    /**
     * IO线程执行
     */
    public static void ioThreadExecute(Runnable runnable) {
        if (isMainThread()){
            getInstance().mDiskIO.execute(runnable);
        }
        else {
            runnable.run();
        }
    }

    public static void delay(long delayMillis) {
        try {
            Thread.sleep(delayMillis);
        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
            e.printStackTrace();
        }
    }


}
